package com.api.customization.unionpay.market.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.entity.Response;
import com.customization.unionpay.market.service.MarketActivityEventService;
import com.customization.unionpay.market.service.impl.MarketActivityEventServiceImpl;
import com.icbc.api.internal.util.internal.util.fastjson.JSON;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

/**
 * 营销活动事项台账接口
 */
@Path("/market/acitivity")
public class MarketActivityEventController {


    /**
     * 财务事项完结
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/financialMatters")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public String financialMatters(@Context HttpServletRequest request,
                                   @Context HttpServletResponse response,
                                    String requestinfo) {
        MarketActivityEventService service = new MarketActivityEventServiceImpl();
        User user = HrmUserVarify.checkUser(request,response);
//        Map<String,Object> map = ParamUtil.request2Map(request);
//        List<String> ids = Arrays.asList(map.get("ids").toString().split(","));
//        JSONObject jsonObject  = JSONObject.parseObject(requestinfo);
        JSONArray arr = JSONArray.parseArray(requestinfo);
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for (int i=0; i<arr.size(); i++) {
            JSONObject currentObj = arr.getJSONObject(i);
            String id = currentObj.getString("id");
            String bdh = currentObj.getString("bdh");
            JSONObject newObj = new JSONObject();
            newObj.put("id", id);
            newObj.put("bdh", bdh);
            jsonObjectList.add(newObj);
        }
        String result = service.financialMattersOver(jsonObjectList,user);
        return JSON.toJSONString(Response.success(result));
    }
}
